using NodeCanvas.Variables;
using UnityEngine;

namespace NodeCanvas.Actions
{
	[Category("Animation")]
	[AgentType(typeof(Animation))]
	public class PlayAnimationAdvanced : ActionTask
	{
		public enum PlayDirections
		{
			Forward = 0,
			Backward = 1,
			Toggle = 2
		}

		[RequiredField]
		public AnimationClip animationClip;

		public WrapMode animationWrap;

		public AnimationBlendMode blendMode;

		[SliderField(0f, 2f)]
		public float playbackSpeed = 1f;

		[SliderField(0f, 1f)]
		public float crossFadeTime = 0.25f;

		public PlayDirections playDirection;

		public BBString mixTransformName;

		public BBInt animationLayer;

		public bool queueAnimation;

		public bool waitUntilFinish = true;

		private string animationToPlay = string.Empty;

		private int dir = -1;

		private Transform mixTransform;

		protected override string info
		{
			get
			{
				return "Anim '" + ((!animationClip) ? "NULL" : animationClip.name) + "'";
			}
		}

		protected override void OnExecute()
		{
			if (playDirection == PlayDirections.Toggle)
			{
				dir = -dir;
			}
			if (playDirection == PlayDirections.Backward)
			{
				dir = -1;
			}
			if (playDirection == PlayDirections.Forward)
			{
				dir = 1;
			}
			base.agent.GetComponent<Animation>().AddClip(animationClip, animationClip.name);
			animationToPlay = animationClip.name;
			if (!string.IsNullOrEmpty(mixTransformName.value))
			{
				mixTransform = FindTransform(base.agent.transform, mixTransformName.value);
				if (!mixTransform)
				{
					Debug.LogWarning("Cant find transform with name '" + mixTransformName.value + "' for PlayAnimation Action", base.gameObject);
				}
			}
			else
			{
				mixTransform = null;
			}
			animationToPlay = animationClip.name;
			if ((bool)mixTransform)
			{
				base.agent.GetComponent<Animation>()[animationToPlay].AddMixingTransform(mixTransform, true);
			}
			base.agent.GetComponent<Animation>()[animationToPlay].layer = animationLayer.value;
			base.agent.GetComponent<Animation>()[animationToPlay].speed = (float)dir * playbackSpeed;
			base.agent.GetComponent<Animation>()[animationToPlay].normalizedTime = Mathf.Clamp01(-dir);
			base.agent.GetComponent<Animation>()[animationToPlay].wrapMode = animationWrap;
			base.agent.GetComponent<Animation>()[animationToPlay].blendMode = blendMode;
			if (queueAnimation)
			{
				base.agent.GetComponent<Animation>().CrossFadeQueued(animationToPlay, crossFadeTime);
			}
			else
			{
				base.agent.GetComponent<Animation>().CrossFade(animationToPlay, crossFadeTime);
			}
			if (!waitUntilFinish)
			{
				EndAction(true);
			}
		}

		protected override void OnUpdate()
		{
			if (base.elapsedTime >= base.agent.GetComponent<Animation>()[animationToPlay].length / playbackSpeed - crossFadeTime)
			{
				EndAction(true);
			}
		}

		private Transform FindTransform(Transform parent, string name)
		{
			if (parent.name == name)
			{
				return parent;
			}
			Transform[] componentsInChildren = parent.GetComponentsInChildren<Transform>();
			Transform[] array = componentsInChildren;
			foreach (Transform transform in array)
			{
				if (transform.name == name)
				{
					return transform;
				}
			}
			return null;
		}
	}
}
